import { show_st, cur_time, pre_cur, find_date_format, obj_str, d_sort, ck_date, loc, add_ck_col, ck_sn, ck_empty, ck_int, cline,
    is_empty, uDb, reload_col } from 'sui';

const fix_state = () => {
    return [
        {sn: "open", name: show_st("open")},
        {sn: "pause", name: show_st("pause")},
        {sn: "close", name: show_st("close")},
    ];
};

const fix_col = () => {
    return [
        {sn: "sn", name: loc("l_315"), tp: "string"},
        {sn: "tech", name: loc("l_132"), tp: "string", ctp: "list"},
        {sn: "state", name: loc("l_352"), tp: "string", ctp: "list"},
        {sn: "pstart", name: loc("l_676"), tp: "string", ctp: "datetime"},
        {sn: "pend", name: loc("l_677"), tp: "string", ctp: "datetime"},
        {sn: "pnum", name: loc("l_146"), tp: "num"},
    ];
};

const fix_psn = () => {
    return [
        {sn: "sn", name: loc("l_152"), tp: "string"},
    ];
};

const load_col = (setCol) => {
    const fix = fix_col();
    reload_col(null, v => setCol(v), "task", fix);
};

const reload = (hDone, setMain, s, e) => {
    uDb(hDone, "task/list",
        {
            line: cline(),
            s: s,
            e: e,
            count: true,
        },
        data => {
            if (is_empty(data)) {
                setMain(null);
                return;
            }

            d_sort(data, "pstart", true);
            data.forEach((d, i) => d["index"] = i + 1);

            setMain(data);
        }, e => setMain(null));
};

const do_md = (umd, setMain, s, e, v, hcmd) => {
    umd.startWait();

    uDb(null, "task/md",
        {
            line: cline(),
            k: obj_str(v, "sn"),
            v: v,
        },
        r => {
            reload(umd.endWait, setMain, s, e);

            if (hcmd) {
                hcmd();
            }
        }, e => umd.setErr(loc("l_232")), true);
};

const do_add = (umd, setMain, s, e, v, hcmd) => {
    umd.startWait();

    uDb(null, "task/add",
        {
            line: cline(),
            v: v,
        },
        r => {
            reload(umd.endWait, setMain, s, e);

            if (hcmd) {
                hcmd();
            }
        }, e => umd.setErr(loc("l_232")), true);
};

const load_tech = (setTech) => {
    uDb(null, "tech/list",
        {
            line: cline(),
        },
        data => {
            if (is_empty(data)) {
                setTech(null);
                return;
            }

            setTech(data);
        }, e => setTech(null));
};

const get_ck = (cur, main, col) => {
    const ret = {
        sn : v => ck_sn(v, cur, main),
        tech: v => ck_empty(v),
        state: v => ck_empty(v),
        pnum: v => ck_int(v),
        pstart: v => ck_date(v),
        pend: (v) => ck_date(v),
    };

    add_ck_col(ret, col);

    return ret;
};

const get_find_tm = () => {
    let s = pre_cur(-180);
    let e = cur_time();

    s = new Date(s);
    e = new Date(e);

    const t = find_date_format(s, e, "datetime");

    return [s, e, t];
};

const normal_task = (sn) => /^\d/.test(sn);

const ready_task = (sn, bsn) => {
    if (!normal_task(sn)) {
        return false;
    }

    return !bsn;
}

const repair_task = (sn) => sn.startsWith("R");

const group_task = (sn) => sn.startsWith("G");

const auto_task = (sn) => sn.startsWith("A");

const old_task = (sn) => sn.startsWith("old");

export {old_task, auto_task, group_task, repair_task, ready_task, get_find_tm, fix_psn, do_add, do_md, fix_state, get_ck, load_tech, reload, load_col, fix_col};
